package com.adobe.training.core;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class TestVersionInNode {

    private static final String NODE_VERSION_PATH = "/content/geometrixx/en/products/triangle/jcr:content/title";
    private static final Logger LOGGER = LoggerFactory.getLogger(TestVersionInNode.class);

    @Reference
    private SlingRepository repository;

    @Activate
    protected void activate() {
        LOGGER.info("Test Version activated");
        TestVersion();
    }

    @SuppressWarnings("deprecation")
    private void TestVersion() {
        Session session = null;
        try {
            session = repository.loginAdministrative(null);
            if(session.itemExists(NODE_VERSION_PATH)) { 
                Node node = session.getNode(NODE_VERSION_PATH);
                node.checkout();
                node.setProperty("jcr:title", "test4");
                node.save();
                node.checkin();
                node.restore("1.1", false);
            }
        }
        catch (RepositoryException e) { 
                LOGGER.error("exception during creating version ", e);
            } 
        finally {
            if (session != null) {
                session.logout();
            }
        }
        
    }
}
